// pages/my/team/index.js
const app = getApp()
const api = require('../../../fetch/api')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabs: ['全部','一级','二级'],
    tabIndex: 0,
    arr: [],
    loading: false,
    finished: false,
    page: 1
  },
  clear() {
    this.setData({
      arr: [],
      page: 1,
      loading: false,
      finished: false
    })
  },
  switchTab(e) {
    const {
      index
    } = app.dataSet(e)
    if (this.data.tabIndex != index) {
      this.setData({
        tabIndex: index,
      })
      this.clear()
      this.team()
    }
  },
  async team() {
    if (!this.data.loading) {
      this.setData({
        loading: true
      })
      let res = await api.team({
        page: this.data.page,
        type: this.data.tabIndex
      })
      let finished = false
      res.data.length == 0 && (finished = true)
      this.setData({
        arr: [...this.data.arr,...res.data],
        finished,
        loading: false
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.clear()
    this.team()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let {
      page,
      finished
    } = this.data
    if (!finished) {
      this.setData({
        page: page + 1
      })
      this.team()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})